<?php

namespace App\Http\Controllers\Apps;

use App\Http\Controllers\AppController;
use App\Http\Controllers\Controller;
use App\Http\Resources\Apps\ProductAuctionResource;
use App\Models\Order;
use App\Models\ProductAuction;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class Product extends AppController
{
    public function auction(Request $request)
    {
        $auction = ProductAuction::where('status','<',2)->get(['id','start','continue']);

        collect($auction)->transform(function ($item){

            $item->second = strtotime($item->start) - strtotime(date('H:i:s'));

            return $item;
        });

        $unplayed = collect($auction)->where('start','>',date('H:i:s'))->toArray();

        $underway = collect($auction)->where('start','<',date('H:i:s'))->map(function ($item){

            if ($item->second + ($item->continue * 60) > 0){
                $item->second = $item->second + ($item->continue * 60);

                return $item;
            }

            return false;
        })->filter();


        return $this->ReturnSuccessInfo([
            'unplayed'  => count($unplayed) ? collect($unplayed)->first() : [],
            'underway'  => count($underway) ? collect($underway)->first() : [],
        ]);
    }

    public function auctionProduct(Request $request)
    {
        $validator = Validator::make($post = $request->all(),[
            'auction_id'    => 'required|integer',
        ]);

        if ($validator->fails()){

            return $this->ReturnFailed(1,$validator->errors());
        }

        $auctionProduct = collect(Order::whereIn('status',[0,1])->get(['product_id']))->pluck('product_id');

        $elem = \App\Models\Product::latest()
                ->where('transfer',1)
                ->where('pick',0)
                ->whereNotIn('id',$auctionProduct);

        $data = $elem->paginate($request->input('num',10));

        return $this->ReturnPage($data);
    }

    public function buy(Request $request)
    {
        $validator = Validator::make($post = $request->all(),[
            'auction_id'    => 'required|integer',
            'product_id'    => 'required|integer'
        ]);

        if ($validator->fails()){

            return $this->ReturnFailed(1,$validator->errors());
        }

        $auction = ProductAuction::find($post['auction_id']);

        if ($auction['start'] > date('H:i:s')){

            return $this->ReturnVerifyError(21);
        }else{

            if ($auction['start'] < date('H:i:s',strtotime('-'.$auction['continue'].'minute'))){

                return $this->ReturnVerifyError(22);
            }
        }

        $product = \App\Models\Product::find($post['product_id']);

        $order = Order::whereIn('status',[0,1])
                    ->where('product_id',$post['product_id']);

        if ($order->exists()){

            return $this->ReturnVerifyError(23);
        }

        if ($this->auth()->user()->balance < $product['integral']){

            return $this->ReturnVerifyError(26);
        }

        Order::create([
            'order_no'      => date('YmdHis').$this->auth()->id().rand(1000,999),
            'sale_user_id'  => $product->user_id,
            'buy_user_id'   => $this->auth()->id(),
            'auction_id'    => $post['auction_id'],
            'product_id'    => $post['product_id'],
            'amount'        => $product['price'],
            'integral'      => $product['integral']
        ]);

        return $this->ReturnSuccessInfo();
    }
}
